package itman.elm.dao.impl;

import itman.elm.dao.FoodDao;
import itman.elm.po.Food;
import itman.elm.utils.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.PrimitiveIterator;

public class FoodDaoImpl implements FoodDao {
    private Connection conn = null;
    private PreparedStatement pstm = null;
    private ResultSet rs = null;

    @Override
    public List<Food> listFoodByBusinessId(Integer businessId) throws Exception {
        List<Food> list = new ArrayList<>();
        String sql = "select * from food where businessId=? order by foodId";
        try {
            conn = DBUtil.getConnection();
            pstm = conn.prepareStatement(sql);
            pstm.setInt(1, businessId);
            rs = pstm.executeQuery();
            while (rs.next()){
                Food food = new Food();
                food.setFoodId(rs.getInt("foodId"));
                food.setFoodName(rs.getString("foodName"));
                food.setFoodExplain(rs.getString("foodExplain"));
                food.setFoodImp(rs.getString("foodImp"));
                food.setFoodPrice(rs.getDouble("foodPrice"));
                food.setBusinessId(rs.getInt("businessId"));
                food.setRemarks(rs.getString("remarks"));
                list.add(food);
            }
        }catch (Exception e){
            e.printStackTrace();
        } finally {
            DBUtil.close(rs,pstm);
        }
        return list;
    }
}
